package com.du.web.sample.model;

import lombok.Data;

import java.io.Serializable;

/**
 * @desc:
 * @author: shuangdu
 * @date: 2021/8/5
 * @since: 1.8
 */
@Data
public class ResBean<T> implements Serializable {
    // 成功
    public static final int SUCCESS = 0;
    // 失败
    public static final int FAIL = 1;

    public static final String DEFAULT_MSG = "操作成功";

    public static final String DEFAULT_ERROR_MSG = "操作失败";

    private String msg;

    private Integer code;

    private T data;

    private Boolean flag;

    public ResBean() {
        this(FAIL, DEFAULT_ERROR_MSG, null, Boolean.FALSE);
    }

    public ResBean(int code, String msg) {
        this(code, msg, null, Boolean.FALSE);
    }

    public ResBean(int code, String msg, T data, Boolean flag) {
        super();
        this.code = code;
        this.data = data;
        this.msg = msg;
        this.flag = flag;
    }

    public ResBean(Throwable e) {
        this(e, FAIL);
    }

    public ResBean(Throwable e, int code) {
        super();
        this.msg = e.getMessage();
        this.code = code;
    }

    public static <T> ResBean<T> ok() {
        return ok(null);
    }

    public static <T> ResBean<T> ok(T data) {
        return new ResBean<T>(SUCCESS, DEFAULT_MSG, data, Boolean.TRUE);
    }

    private static <T> ResBean<T> error() {
        return error(DEFAULT_ERROR_MSG, null);
    }

    public static <T> ResBean<T> error(String msg) {
        return error(msg, null);
    }

    public static <T> ResBean<T> error(T data) {
        return error(DEFAULT_ERROR_MSG, data);
    }

    public static <T> ResBean<T> error(String msg, T data) {
        return new ResBean<>(FAIL, msg, data, Boolean.FALSE);
    }
}
